package org.alex.samples.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.alex.samples.model.ExamTestVO;
import org.alex.samples.model.MessageTest;
import org.alex.samples.model.User;
import org.alex.samples.service.ITestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class TestController {
	
	@Autowired
	private ITestService testService;
	@Autowired
	private MessageSource messageSource;
	
	@RequestMapping("/test")
	public String test(Model model) {
		List<ExamTestVO> list = testService.loadAllTest();
		model.addAttribute("testList",list);
		return "test/testList";
	}
	@RequestMapping(value = "/toTest/{subId}",method = RequestMethod.GET)
	public String toTest(Model model,HttpSession session,@PathVariable Integer subId){
		User user = (User) session.getAttribute("User");
		List<ExamTestVO> examTestVO= testService.getTestListBySubjectExample(subId, user.getId());
		
		model.addAttribute("studentTestList",examTestVO);
		return "test/joinTest";
	}
	@RequestMapping(value = "/assignment",method = RequestMethod.POST)
	public String toTest(HttpServlet request,String userId,Integer subId,Model model,RedirectAttributes redirectAttr){
		
		
		redirectAttr.addFlashAttribute("message","提交成功");
		return "sub/studentList";
	}
	
	@ResponseBody
	@RequestMapping(value="/test/getTestById",method=RequestMethod.POST)
	public ExamTestVO getTestById(Integer testId){
		ExamTestVO resultVO = testService.getTestForId(testId);
		return resultVO;
	}
//	@ResponseBody
//	@RequestMapping(value="/test/findNameAndType",method=RequestMethod.POST)
//	public Map<String,Object> getSuNameAndType(){
//		List<ExamTestTypeVO> testTypeList = testService.getTestType();
//		List<ExamSubjectVO> subjectList = testService.getSubject();
//		Map<String,Object> resultMap = new HashMap<String,Object>();
//		resultMap.put("testTypeList",testTypeList );
//		resultMap.put("subjectList", subjectList);
//		return resultMap;
//	}
	@ResponseBody
	@RequestMapping(value="/test/saveTest",method=RequestMethod.POST)
	public MessageTest saveTest(@Valid ExamTestVO examTestVO,BindingResult result){
		MessageTest messageTest = new MessageTest();
		Map<String,String> messageMap = new HashMap<String,String>();
		messageTest.setSuccess(false);
		if(result.hasErrors()){
			List<ObjectError> errorList = result.getAllErrors();
			for(ObjectError error : errorList){
				FieldError fe = (FieldError) error;
				messageMap.put(fe.getField(), fe.getDefaultMessage());
			}
			messageTest.setMessageMap(messageMap);
			messageTest.setMessage("添加失败");
		}
		try{
			testService.addTest(examTestVO);
			messageTest.setMessage("添加成功");
		}catch(Exception e){
			messageTest.setSuccess(false);
			messageTest.setMessage("添加失败");
		}
		return messageTest;
	}
//	@ResponseBody
//	@RequestMapping(value="/test/editTest",method=RequestMethod.POST)
//	public Map<String,Object> editTest(Integer testId){
//		List<ExamTestTypeVO> testTypeList = testService.getTestType();
//		List<ExamSubjectVO> subjectList = testService.getSubject();
//		ExamTestVO testVO = testService.getTestForId(testId);
//		Map<String,Object> resultMap = new HashMap<String,Object>();
//		resultMap.put("testTypeList", testTypeList);
//		resultMap.put("subjectList", subjectList);
//		resultMap.put("testVO", testVO);
//		return resultMap;
//	}
//	@ResponseBody
//	@RequestMapping(value="/test/toEditTest",method=RequestMethod.POST)
//	public MesageTest toEditTest(ExamTestVO examTestVO){
//		MessageTest messageTest = new MessageTest();
//		try{
//			testService.editTest(examTestVO);
//			messageTest.setIsOK("true");
//			messageTest.setMessage("修改成功");
//		}catch(Exception e){
//			messageTest.setIsOk("false");
//			messageTest.setMessage("修改失败");
//		}
//		return messageTest;
//	}
//	@ResponseBody
//	@RequestMapping(value="/test/deleteTest",method=RequestMethod.POST)
//	public MessageTest deleteTest(Integer testId){
//		MessageTest messageTest = new MessageTest();
//		messageTest.setIsOK("false");
//		try{
//			testService.delete(testId);
//			messageTest.setIsOK("true");
//			messageTest.setMessage("删除成功");
//		}catch(Exception e){
//			messageTest.setIsOk("false");
//			messageTest.setMessage("删除失败");
//		}
//		return messageTest;
//	}
	
	
	
	
	

}
